package stephencarmody.random_rotating_triangles;

import stephencarmody.k8.core.Logic;
import stephencarmody.k8.core.SceneNode;
import stephencarmody.k8.util.LinkedList;

public class ConstantRotation extends Logic {
    
	private static LinkedList<ConstantRotation> rotators = new LinkedList<ConstantRotation>();
	private static LinkedList.Node<ConstantRotation> curr;
	
	private static int updaterate = 10;
	private static boolean paused = false;
	
    private float pitch;
    private float yaw;
    private float roll;
    private SceneNode scenenode;
    private long lastupdate;
    private long elapsed;
    
    public static void pauseAll() {
    	curr = null;
    	if ( paused )
    		while ( (curr = rotators.next(curr)) != null ) {
    			curr.item.setUpdateRate(updaterate);
    			curr.item.lastupdate = System.nanoTime();
    		}
    	else
    		while ( (curr = rotators.next(curr)) != null ) curr.item.setUpdateRate(0);
    	paused = !paused;
    }
    
    /** Creates a new instance of RotatingSquare */
    public ConstantRotation(float pitch, float yaw, float roll) {
    	if ( !paused ) setUpdateRate(updaterate);
        this.pitch = pitch;
        this.yaw = yaw;
        this.roll = roll;
        rotators.append(this);
    }

    /** @see stephencarmody.Application.core.ListNode */
    public void update(long time) {
        // Calculate elapsed time
        elapsed = time - lastupdate;
        lastupdate = time;
        
    	scenenode.pitch(pitch*elapsed);
    	scenenode.yaw(yaw*elapsed);
    	scenenode.roll(roll*elapsed);
    }
    
    public void setSceneNode(SceneNode scenenode) {
    	this.scenenode = scenenode;
    }
    
    public SceneNode getSceneNode() {
    	return scenenode;
    }
}
